package com.example.a66666.yuejian.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a66666.yuejian.Bean.LoginBean;
import com.example.a66666.yuejian.Bean.MasterBean;
import com.example.a66666.yuejian.Listener.RetrofitListener;
import com.example.a66666.yuejian.Model.UserModel;
import com.example.a66666.yuejian.R;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

public class MasterDetailActivity extends AppCompatActivity {
    private TextView masterName,masterPhone,masterIntro,collect,back;
    private ImageView imageView;
    private RetrofitListener<MasterBean> listener;
    private RetrofitListener<LoginBean> isCollectListener,collectListener,cancelCollectListener;
    private UserModel userModel,isCollectUserModel,collectUserModel,cancelCollectUserModel;
    private SharedPreferences sp;
    private int uid;
    private int masterID;
    private int isCollected=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_master_detail);
        sp=getSharedPreferences("me", MODE_PRIVATE);
        uid=sp.getInt("userid",6666);
        initView();
        initListener();
        initClick();
        getData();
    }

    public void initView(){
        masterName=(TextView)findViewById(R.id.name);
        masterPhone=(TextView)findViewById(R.id.phone);
        masterIntro=(TextView)findViewById(R.id.intro);
        collect=(TextView)findViewById(R.id.collect);
        back=(TextView)findViewById(R.id.back);
        imageView=(ImageView)findViewById(R.id.master_picture);

    }

    public void initListener(){
        listener=new RetrofitListener<MasterBean>() {
            @Override
            public void onSuccess(MasterBean masterBean) {
                masterID=masterBean.getMid();
                masterName.setText(masterBean.getMname());
                masterPhone.setText(masterBean.getMphone());
                masterIntro.setText(masterBean.getMintro());
                Picasso.with(imageView.getContext()).load("http://10.0.2.2/yuejian/public/static/masterpic/"+masterBean.getMpic()).into(imageView);
                isCollectUserModel=new UserModel();
                isCollectUserModel.isCollectMaster(uid,masterID,isCollectListener);
            }

            @Override
            public void onFail(String msg) {

            }
        };

        isCollectListener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                if(loginBean.getFlag()==1){
                    collect.setText("取消关注");
                    isCollected=1;
                }
                else{
                    collect.setText("关注");
                    isCollected=0;
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        collectListener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                if(loginBean.getFlag()==1){
                    Toast.makeText(MasterDetailActivity.this,"关注成功",Toast.LENGTH_LONG).show();
                    collect.setText("取消关注");
                    isCollected=1;
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        cancelCollectListener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                if(loginBean.getFlag()==1){
                    Toast.makeText(MasterDetailActivity.this,"取消关注成功",Toast.LENGTH_LONG).show();
                    collect.setText("关注");
                    isCollected=0;
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };
    }

    public void getData(){
        userModel=new UserModel();
        userModel.getMasterDetail(getIntent().getIntExtra("masterID",1),listener);
    }

    public void initClick(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MasterDetailActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });

        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isCollected==1){
                    cancelCollectUserModel=new UserModel();
                    cancelCollectUserModel.cancelCollectMaster(uid,masterID,cancelCollectListener);
                }
                else{
                    collectUserModel=new UserModel();
                    collectUserModel.collectMaster(uid,masterID,collectListener);
                }
            }
        });
    }
}
